﻿using System;
using System.Collections.Generic;
using System.Web;

namespace TheOutfield.UmbExt.UniversalMediaPicker.Providers.FileSystem
{
    public class FileSystemProvider : AbstractProvider
    {
        FileSystemService _service;
        FileSystemConfigControl _configControl;

        public FileSystemProvider()
        { }

        public FileSystemProvider(string config)
            : base(config)
        {
            _service = new FileSystemService(config);
        }

        public override string Alias
        {
            get { return "FileSystem"; }
        }

        public override Interfaces.IConfigControl ConfigControl
        {
            get
            {
                if (_configControl == null)
                    _configControl = new FileSystemConfigControl(Config);

                return _configControl;
            }
        }

        public override Interfaces.ICreateControl CreateControl
        {
            get { throw new NotImplementedException(); }
        }

        public override MediaItem GetMediaItemById(string id)
        {
            var file = _service.GetFile(id);
            return file == null ? null : file.ToMediaItem(_service.RootDirPath);
        }

        public override IList<MediaItem> GetMediaItems(string parentId)
        {
            var mediaItems = new List<MediaItem>();

            var dirs = _service.GetDirectories(parentId);
            foreach (var dir in dirs)
            {
                mediaItems.Add(dir.ToMediaItem(_service.RootDirPath));
            }

            var files = _service.GetFiles(parentId);
            foreach (var file in files)
            {
                mediaItems.Add(file.ToMediaItem(_service.RootDirPath));
            }

            return mediaItems;
        }
    }
}